package cc.linker.steplink.crm.merchant.service;

import cc.linker.steplink.crm.merchant.dao.ShopGroupDao;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopGroupService {

    public static final String GROUP_BELONG_ID = "SCRM:GROUP_BELONG_ID";
    @Autowired
    private ShopGroupDao shopGroupDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    public Object list(Map<String, Object> map) {
        Integer pageIndex = (Integer) map.get("pageIndex");
        Integer pageSize = (Integer) map.get("pageSize");
        boolean isFirst = false;
        pageIndex--;
        if (pageIndex == 0) {
            pageSize -= 2;
            isFirst = true;
        } else {
            pageIndex = pageIndex * pageSize - 2;
        }
        PageHelper.offsetPage(pageIndex, pageSize);
        List<Map<String, Object>> list = shopGroupDao.list(map);
        if (list == null) {
            list = new ArrayList<>();
        }
        if (isFirst) {
            Map<String, Object> map0 = new HashMap<>();
            map0.put("id", -1);
            map0.put("groupName", "全部");
            map0.put("createTime", "-");
            Integer allGoodsNum = shopGroupDao.countAllGoodsNum(map);
            map0.put("goodsNum", allGoodsNum);
            list.add(0, map0);
            Map<String, Object> map1 = new HashMap<>();
            map1.put("id", 0);
            map1.put("groupName", "默认分组");
            map1.put("createTime", "-");
            Integer defaultGoodsNum = shopGroupDao.countDefaultGoodsNum(map);
            map1.put("goodsNum", defaultGoodsNum);
            list.add(1, map1);
        }
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(list);
        mapPageInfo.setTotal(mapPageInfo.getTotal() + 2);
        return mapPageInfo;
    }

    public void del(String groupId) {
        Map<String, Object> group = shopGroupDao.getGroupByGroupId(groupId);
        if (group == null) {
            throw GlobalException.build("无此分组");
        }
        String belongId = (String) group.get("belongId");
        redisTemplate.opsForHash().put(GROUP_BELONG_ID, groupId, belongId);
        shopGroupDao.del(groupId);
        shopGroupDao.delRelation(groupId);
        shopGroupDao.delOrgRelation(groupId);
        this.changeToDefault();
    }

    @Async
    public void changeToDefault() {
        List<Long> goodsId = shopGroupDao.getGoodsWhereNotHaveGroup();
        if (goodsId != null && goodsId.size() > 0) {
            shopGroupDao.insertDefaultBatch(goodsId);
        }
    }

    public void insert(Map<String, Object> map) throws GlobalException {
        if (!map.containsKey("groupType") || map.get("groupType") == null) {
            map.put("groupType", 1);
        }
        int count = shopGroupDao.count(map);
        Object groupName = map.get("groupName");
        if (count > 0 || "默认分组".equals(groupName)) {
            throw GlobalException.build("分组名称已存在");
        }
        shopGroupDao.insert(map);
    }

    public void edit(Map<String, Object> map) throws GlobalException {
        Integer groupId = (Integer) map.get("groupId");
        String groupName = (String) map.get("groupName");
        if (StringUtils.isEmpty(groupId) || StringUtils.isEmpty(groupName)) {
            throw GlobalException.build("参数不能为空");
        }
        int count = shopGroupDao.count(map);
        if (count > 0 || "默认分组".equals(groupName)) {
            throw GlobalException.build("分组名称已存在");
        }
        shopGroupDao.edit(map);

    }

    public List<Map<String, Object>> listAll(String orgId, String customerId, String type, Integer groupType) {
        List<Map<String, Object>> list = shopGroupDao.listAll(orgId, customerId, type, groupType);
        Map<String, Object> map = new HashMap<>();
        map.put("id", 0);
        map.put("groupName", "默认分组");
        list.add(0, map);
        return list;
    }
}
